package com.auto.addfrinend;

import android.accessibilityservice.AccessibilityService;
import android.content.Intent;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;

import static com.auto.addfrinend.MainActivity.j;

/**
 * User: Moon
 * Data: 2017/3/7.
 */

public class AutoAddFriends extends AccessibilityService {

    public static boolean hasSend = false;

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Toast.makeText(this, "自动加好友服务链接上了", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        //如果手机当前界面的窗口发送变化
        if (accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {

            //获取当前activity的类名:
            String currentWindowActivity = accessibilityEvent.getClassName().toString();
            Log.e("当前界面", currentWindowActivity);
            if("com.tencent.mm.ui.LauncherUI".equals(currentWindowActivity)){
                if (!hasSend) {
                    Utils.findTextAndClick(this, "搜索");
                } else {
                    Intent intent = getPackageManager().getLaunchIntentForPackage("com.auto.addfrinend");
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("hasRun", true);
                    startActivity(intent);
                }

            } else if ("com.tencent.mm.plugin.search.ui.FTSMainUI".equals(currentWindowActivity)) {
                if (!hasSend) {
                    Utils.fillInputBar(MainActivity.list.get(j), this);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Utils.findViewIdAndClick(this, "com.tencent.mm:id/axq");
                } else {
                    Utils.performBack(this);
                    Utils.performBack(this);

                }

            }
            else if ("com.tencent.mm.plugin.profile.ui.ContactInfoUI".equals(currentWindowActivity)) {

                if (!hasSend) {
                    Utils.findTextAndClick(this, "添加到通讯录");
                } else {
                    Utils.performBack(this);
                }
            } else if ("com.tencent.mm.plugin.profile.ui.SayHiWithSnsPermissionUI".equals(currentWindowActivity)) {
                if (!hasSend) {
                    Utils.findTextAndClick(this,"发送");
                }
                hasSend = true;
            } else if ("com.tencent.mm.ui.base.h".equals(currentWindowActivity)) {
                Utils.findViewIdAndClick(this, "com.tencent.mm:id/abg");
                hasSend = true;
            }
        }
    }

    @Override
    public void onInterrupt() {
        Toast.makeText(this, "自动加好友服务断开了", Toast.LENGTH_SHORT).show();
    }
}
